package cn.doitedu.demo4.utils;

import cn.doitedu.demo4.beans.RuleMetaBean;
import cn.doitedu.demo4.functions.CdcMeta2RuleMetaBeanFunction;
import com.ververica.cdc.connectors.mysql.source.MySqlSource;
import com.ververica.cdc.debezium.JsonDebeziumDeserializationSchema;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class MetaDataFetcher {

    public static SingleOutputStreamOperator<RuleMetaBean> fetchMetaData(StreamExecutionEnvironment env){
        // 读取规则元数据表
        MySqlSource<String> metaCdcSource = MySqlSource.<String>builder()
                .hostname("doitedu")
                .port(3306)
                .databaseList("doit42") // set captured database, If you need to synchronize the whole database, Please set tableList to ".*".
                .tableList("doit42.rule_meta") // set captured table
                .username("root")
                .password("root")
                .deserializer(new JsonDebeziumDeserializationSchema()) // converts SourceRecord to JSON String
                .build();

        DataStreamSource<String> cdcStream = env.fromSource(metaCdcSource, WatermarkStrategy.noWatermarks(), "cdc-source");
        SingleOutputStreamOperator<RuleMetaBean> metaBeanStream = cdcStream.map(new CdcMeta2RuleMetaBeanFunction());

        return metaBeanStream;
    }


}
